package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 根据用户名查询员工
     *
     * @param employee
     * @return
     */
    @Override
    public Employee selectByUsername(Employee employee) {
        employeeMapper.selectByUsername(employee);
        Employee resultEmployee = employeeMapper.selectByUsername(employee);
        return resultEmployee;
    }

    @Override
    public void save(Employee employee) {
        employeeMapper.save(employee);
    }

    @Override
    public PageInfo<Employee> selectByPage(int page, int pageSize, String name) {
        //开启分页,通过某种方式改变了SQL语句,后面加了limit  起始索引, 查询记录数
        PageHelper.startPage(/*当前第几页*/page,/*每页多少条*/pageSize);
        //查询所有,只返回指定页
        List<Employee> list = employeeMapper.selectAllByName(name);
        //封装PageInfo
        PageInfo<Employee> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateStatus(employee);
    }

    @Override
    public Employee selectAllById(Integer id) {
        return employeeMapper.selectAllById(id);
    }
}
